package com.zs.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.blog.converter.FriendLinkConverter;
import com.zs.blog.dao.IFriendLinkMapper;
import com.zs.blog.exception.BusinessException;
import com.zs.blog.model.dto.ApplyFriendLinkDTO;
import com.zs.blog.model.dto.AuditFriendLinkDTO;
import com.zs.blog.model.dto.FriendLinkSearchDTO;
import com.zs.blog.model.dto.RemoveFriendLinkDTO;
import com.zs.blog.model.entity.FriendLink;
import com.zs.blog.model.vo.FriendLinkListVO;
import com.zs.blog.model.vo.PageInfoVO;
import com.zs.blog.service.IFriendLinkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Title: IFriendLinkServiceImpl
 * @Author zs
 * @Package com.zs.blog.service.impl
 * @Date 2025/6/3 22:18
 * @description: 友链服务层接口实现
 */
@Service
public class IFriendLinkServiceImpl extends ServiceImpl<IFriendLinkMapper, FriendLink> implements IFriendLinkService {

    public IFriendLinkMapper friendLinkMapper;

    public IFriendLinkServiceImpl(IFriendLinkMapper friendLinkMapper) {
        this.friendLinkMapper = friendLinkMapper;
    }


    /**
     * 获取分页的友链列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return
     */
    @Override
    public PageInfoVO<FriendLinkListVO> listFriendLink(Long pageNum, Long pageSize) {
        // 分页对象page
        Page<FriendLink> page = new Page<>(pageNum, pageSize);

        // 获取友链列表
        LambdaQueryWrapper<FriendLink> lqw = new LambdaQueryWrapper<>();
        List<FriendLink> friendLinkList = friendLinkMapper.selectList(page, lqw);

        // 属性映射
        List<FriendLinkListVO> friendLinkListVO = FriendLinkConverter.INSTANCE.friendLinkList2FriendLinkListVO(friendLinkList);

        PageInfoVO<FriendLinkListVO> pageInfo = new PageInfoVO<>();
        pageInfo.setTotal(page.getTotal());
        pageInfo.setItems(friendLinkListVO);

        return pageInfo;
    }

    /**
     * 下架友链
     *
     * @param removeFriendLinkDTO 下架友链的ID以及下架原因
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFriendLink(RemoveFriendLinkDTO removeFriendLinkDTO) {

        int rows = friendLinkMapper.deleteById(removeFriendLinkDTO.getFlId());

        if (rows <= 0) {
            throw new BusinessException("删除失败");
        }
        // TODO 发送邮件，携带下架原因
        System.out.println("发送邮件");

    }

    /**
     * 通过搜索条件查询分页的友链列表
     *
     * @param friendLinkSearchDTO 友链搜索条件实体类
     * @return 分页的友链列表
     */
    @Override
    public PageInfoVO<FriendLinkListVO> listFriendLinkBySearchCondition(FriendLinkSearchDTO friendLinkSearchDTO) {
        // 获取分页对象
        Page<FriendLink> page = new Page<>(friendLinkSearchDTO.getPageNum(), friendLinkSearchDTO.getPageSize());

        // 构造SQL条件
        LambdaQueryWrapper<FriendLink> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(friendLinkSearchDTO.getFlName()), FriendLink::getFlName, friendLinkSearchDTO.getFlName());
        lqw.eq(StringUtils.isNotBlank(friendLinkSearchDTO.getEmail()), FriendLink::getEmail, friendLinkSearchDTO.getEmail());
        if (friendLinkSearchDTO.getStartTime() != null && friendLinkSearchDTO.getEndTime() != null) {
            lqw.between(FriendLink::getCreateTime, friendLinkSearchDTO.getStartTime(), friendLinkSearchDTO.getEndTime());
        }
        // 查询
        List<FriendLink> friendLinkList = friendLinkMapper.selectList(lqw);

        // 映射属性值
        List<FriendLinkListVO> friendLinkListVOS = FriendLinkConverter.INSTANCE.friendLinkList2FriendLinkListVO(friendLinkList);

        // 组合返回数据
        PageInfoVO<FriendLinkListVO> pageInfo = new PageInfoVO<>();
        pageInfo.setTotal(page.getTotal());
        pageInfo.setItems(friendLinkListVOS);

        return pageInfo;
    }

    /**
     * 审核友链
     *
     * @param auditFriendLinkDTO 审核友链(友链ID和状态)实体类
     */
    @Override
    public void auditFriendLink(AuditFriendLinkDTO auditFriendLinkDTO) {
        // 构造SQL条件
        LambdaUpdateWrapper<FriendLink> luw = new LambdaUpdateWrapper<>();
        luw.set(FriendLink::getStatus, auditFriendLinkDTO.getStatus());
        luw.eq(FriendLink::getId, auditFriendLinkDTO.getFlId());
        // 更新
        int rows = friendLinkMapper.update(luw);

        if (rows <= 0) {
            throw new BusinessException("友链审核失败");
        }

        // TODO 发送邮件,审核通过
        System.out.println("审核通过");
    }

    /**
     * 申请友链
     *
     * @param applyFriendLinkDTO 申请友链参数实体类
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyFriendLink(ApplyFriendLinkDTO applyFriendLinkDTO) {

        // 数据映射
        FriendLink friendLink = FriendLinkConverter.INSTANCE.applyFriendLinkDTO2FriendLink(applyFriendLinkDTO);

        // TODO 获取登录用户ID

        // 插入友链信息
        int affectedRow = friendLinkMapper.insert(friendLink);
        if (affectedRow <= 0) {
            throw new BusinessException("友链申请提交失败");
        }

        // TODO 给站长发送邮件


    }
}
